
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;

public class ManualThreshold implements ActionListener, ChangeListener
{
	IPWindow window;
	JFrame frame;
	JPanel mainPane;
	JTextField threshTextField;

	public ManualThreshold(IPWindow _window)
	{
		window = _window;
		Color bgColor = new Color(255,255,255);
		
		// Create our frame, panel and buttons.
		frame = new JFrame("Threshold");
		mainPane = new JPanel(new BorderLayout());

		// Populate panel
		mainPane.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));

		// Histogram panel
		JPanel histPanel = new JPanel();
		histPanel.setBorder(
				BorderFactory.createCompoundBorder(
					BorderFactory.createTitledBorder("Histogram"),
					BorderFactory.createEmptyBorder(5,5,5,5)
					));
		histPanel.setBackground(bgColor);

		// Threshold panel
		JPanel threshPanel = new JPanel();
		threshPanel.setBorder(
				BorderFactory.createCompoundBorder(
					BorderFactory.createTitledBorder("Threshold"),
					BorderFactory.createEmptyBorder(5,5,5,5)
					));
		threshPanel.setLayout(new BoxLayout(threshPanel, BoxLayout.PAGE_AXIS));

		JPanel threshTopPanel = new JPanel();
		JLabel threshLabel = new JLabel("Value: ");
		threshTopPanel.add(threshLabel);
		threshTextField = new JTextField(10);
		threshTextField.setText("0");
		threshTopPanel.add(threshTextField);
		threshTopPanel.setBackground(bgColor);
		threshPanel.add(threshTopPanel);
		JSlider threshSlider = new JSlider(JSlider.HORIZONTAL, 0, 255, 0);
		threshSlider.setMajorTickSpacing(64);
		threshSlider.setMinorTickSpacing(16);
		threshSlider.setPaintTicks(true);
		threshSlider.setPaintLabels(true);
		threshSlider.setBackground(bgColor);
		threshSlider.addChangeListener(this);
		threshPanel.add(threshSlider);
		
		JButton threshApplyBtn = new JButton("Apply");
		threshApplyBtn.setActionCommand("apply");
		threshApplyBtn.addActionListener(this);
		threshPanel.add(threshApplyBtn);

		threshPanel.setBackground(bgColor);

		// Nav buttons
		JPanel navPane = new JPanel();
		JButton b1 = new JButton("Prev");
		JButton b2 = new JButton("Next");
		JButton b3 = new JButton("Close");
		b1.setActionCommand("prev");
		b2.setActionCommand("next");
		b3.setActionCommand("close");
		b1.addActionListener(this);
		b2.addActionListener(this);
		b3.addActionListener(this);
		navPane.add(b1);
		navPane.add(b2);
		navPane.add(b3);
		navPane.setBackground(bgColor);

		mainPane.add(histPanel, BorderLayout.PAGE_START);
		mainPane.add(threshPanel, BorderLayout.CENTER);
		mainPane.add(navPane, BorderLayout.PAGE_END);
		
		// Set panel as mane pane on frame, and show frame.
		frame.setContentPane(mainPane);
		mainPane.setOpaque(true);
		mainPane.setBackground(bgColor);
		frame.pack();
		//frame.setLocationRelativeTo(window.frame);
		frame.setVisible(true);		

		////
		// Warning messges
		if (window.getROI() == null)
		{
			p.print("Warning: ROI is null.  Verfiy image is loaded.");
		}
	}

	public void actionPerformed(ActionEvent e)
	{
		String cmd = e.getActionCommand();
		if (cmd.equals("apply"))
		{
			//window.getROI().doThreshold(127);
			String value = threshTextField.getText();
			int ival = Integer.parseInt(value);
			window.getROI().doThreshold(ival);
			window.frame.setImageState(1); // show binary image
			window.updateFrame();
		}
		else if (cmd.equals("prev"))
		{
			new ManualPreprocess(window);
			frame.dispose();
		}
		else if (cmd.equals("next"))
		{
			new ManualMassDetect(window);
			frame.dispose();
		}
		else if (cmd.equals("close"))
		{
			frame.dispose();
		}
	}

	public void stateChanged(ChangeEvent e)
	{
		JSlider source = (JSlider)e.getSource();
		int value = (int)source.getValue();
		String sval = "" + value;
		/*
		if (!source.getValueIsAdjusting())
		{
			// Value when user lets go of mouse button.
			p.print(sval);
		}
		*/
		threshTextField.setText(sval);
	}
}
